package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.Host;
import com.luoxiang.socialandroid.utils.CustomClickListener;

import java.util.List;

public class YouxuanRecyAdapter extends RecyclerView.Adapter<YouxuanRecyAdapter.ViewHolder> {
    private List<Host> list;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";

    public YouxuanRecyAdapter(List<Host> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int position, int userid,String videoPath); //传递boolean类型数据给activity
    }

    YouxuanRecyAdapter.OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(YouxuanRecyAdapter.OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    @NonNull
    @Override
    public YouxuanRecyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_youxuan, parent, false);
        YouxuanRecyAdapter.ViewHolder holder = new YouxuanRecyAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull YouxuanRecyAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Host bean = list.get(position);
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.icon_error_img)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_error_img) //url为空的时候,显示的图片
                .error(R.drawable.icon_error_img);//图片加载失败后，显示的图片
        //Glide设置图片圆角角度
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
        RoundedCorners roundedCorners = new RoundedCorners(30);
        RequestOptions options1 = RequestOptions.bitmapTransform(roundedCorners);
        if (bean.getCoverType() == 0) {
            //设置图片
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getIcon())
                    .transform(new CenterCrop(),new RoundedCorners(30))
                    .into(holder.im_youxuan);
            holder.img_isshipin.setVisibility(View.GONE);
            ImageView imageView = (ImageView) holder.im_youxuan;
//            imageView.setScaleType(ImageView.ScaleType.);
        } else {
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getIcon())
                    .transform(new CenterCrop(),new RoundedCorners(30))
                    .into(holder.im_youxuan);
            holder.img_isshipin.setVisibility(View.VISIBLE);
        }

        if (bean.getTitle() == 1) {
            holder.im2_youxuan.setImageResource(R.mipmap.icon_wangpaizhubo);
        } else {
            holder.im2_youxuan.setImageResource(R.mipmap.icon_zuijiaxinxiu);
        }
        if (bean.getOnline() == 2) {
            holder.im1_youxuan.setImageResource(R.mipmap.icon_zailiao);
            holder.im1_youxuan.setVisibility(View.VISIBLE);
        } else if (bean.getOnline() == 3) {
            holder.im1_youxuan.setImageResource(R.mipmap.icon_online);
            holder.im1_youxuan.setVisibility(View.VISIBLE);
        } else {
            holder.im1_youxuan.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                onItemAddClick.onItemClicks(position, bean.getUserId(),bean.getVideoPath());
            }

            @Override
            protected void onFastClick() {
                return;
            }
        });
    }

    @Override
    public int getItemCount() {
        //防空判断
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im_youxuan, img_isshipin, im1_youxuan, im2_youxuan;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            im_youxuan = itemView.findViewById(R.id.im_youxuan);
            im1_youxuan = itemView.findViewById(R.id.im1_youxuan);
            im2_youxuan = itemView.findViewById(R.id.im2_youxuan);
            img_isshipin = itemView.findViewById(R.id.img_isshipin);
        }
    }
}
